package com.shmilyou.dissertationmanagement.biz;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2018/11/23 */

import com.shmilyou.dissertationmanagement.domain.User;
import com.shmilyou.dissertationmanagement.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserService {

    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User queryById(Integer id) {
        Optional<User> user = userRepository.findById(id);
        return user.orElse(null);
    }

    public User save(User user) {
        return
                userRepository.save(user);
    }

    public User login(User user) {
        List<User> result = userRepository.findByEmailAndPassword(user.getEmail(), user.getPassword());
        return result.size() > 0 ? result.get(0) : null;
    }

}
